package prv.pursuivant.render.html;

import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import java.util.StringTokenizer;

import prv.pursuivant.core.AchievType;
import prv.pursuivant.core.AchievementCalculator;
import prv.pursuivant.core.AchievementStatus;
import prv.pursuivant.core.Toon;
import prv.pursuivant.core.impl.KindImpl;
import prv.pursuivant.render.DataRenderer;
import prv.pursuivant.render.OutputGenerator;
import prv.pursuivant.util.DateUtil;

import com.google.inject.Inject;

public class HtmlDataRenderer implements DataRenderer {

	private final AchievementCalculator calculator;
	private final DateUtil dateUtil;

	@Inject
	public HtmlDataRenderer(AchievementCalculator calculator, DateUtil dateUtil) {
		this.calculator = calculator;
		this.dateUtil = dateUtil;
	}

	@Override
	public void renderAchievCell(OutputGenerator generator, Toon toon,
			AchievType criteria, Date when) {
		AchievementStatus status = calculator.getAchievementStatus(toon
				.getName(), criteria.getId(), when);
		String classAttr = "aX";
		if (status.isDone()) {
			Date date = status.getDate();
			classAttr = getCellClass(when, date);
		} else if (status.isDoneImplicit()) {
			Date date = status.getLastDate();
			classAttr = getCellClass(when, date);
		}
		generator.elem("td").attr("class", classAttr);
		renderStatus(generator, status);
		generator.end();
	}

	/**
	 * @param now
	 * @param date
	 * @return
	 */
	private String getCellClass(Date now, Date date) {
		String classAttr;
		long span = now.getTime() - date.getTime(); // milliseconds
		span /= 1000; // seconds
		span /= 60;// minutes
		span /= 60; // hours
		span /= 24; // days
		if (span < 8)
			classAttr = "a" + span;
		else
			classAttr = "a7";
		return classAttr;
	}

	@Override
	public void renderAchievText(OutputGenerator generator, Toon toon,
			AchievType criteria, Date when) {
		AchievementStatus status = calculator.getAchievementStatus(toon
				.getName(), criteria.getId(), when);
		renderStatus(generator, status);
	}

	private void renderStatus(OutputGenerator generator,
			AchievementStatus status) {
		if (status.isDone())
			generator.text(dateUtil.formatShort(status.getDate()));
		else if (status.getDoneCriteriaCount() > 0)
			generator.text(status.getDoneCriteriaCount() + "/"
					+ status.getAllCriteriaCount());
		else
			generator.entRef("nbsp");
	}

	@Override
	public void renderCriteriaText(OutputGenerator generator,
			AchievType criteria) {
		// String formattedName = formatCriteriaName(criteria.getName());
		if (criteria.getKind().equals(KindImpl.NORMAL)
				|| criteria.getKind().equals(KindImpl.META)) {
			generator.elem("a");
			generator.attr("href", "http://www.wowhead.com/?achievement="
					+ criteria.getId());
			// generator.text(formattedName);
			generator.emptyElem("img").attr("src",
					"../icons/achiev." + criteria.getId() + ".jpg").attr(
					"width", "36").attr("height", "36").attr("alt",
					criteria.getName());
			generator.end();
		} else
			generator.text(criteria.getName());
		// generator.emptyElem("img").attr("src",
		// "../icons/achiev." + criteria.getId() + ".jpg").attr(
		// "width", "36").attr("height", "36").attr("alt",
		// criteria.getName());
		generator.text("");
	}

	private Properties dict;

	@Override
	public String formatCriteriaName(String critName) {
		if (dict == null) {
			dict = new Properties();
			try {
				dict.load(getClass().getResourceAsStream(
						"wordbreaks.properties"));
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		StringTokenizer st = new StringTokenizer(critName, " ", true);
		StringBuilder sb = new StringBuilder();
		while (st.hasMoreTokens()) {
			String s = st.nextToken();
			s = dict.getProperty(s, s);
			sb.append(s);
		}
		return sb.toString();
	}

	@Override
	public void renderToonText(OutputGenerator generator, Toon toon) {
		generator.text(toon.getName());
	}

}
